﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CalendarConverter
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        //当程序被加载时，执行以下操作
        //1、将转换类型设置为“to Chinese Calendar”
        //2、显示今天的农历和公历日期
        private void frmMain_Load(object sender, EventArgs e)
        {
            cbbConvertClass.SelectedIndex = 0;
            int intTodayYear = Convert.ToInt32(DateTime.Now.Year);
            int intTodayMonth = Convert.ToInt32(DateTime.Now.Month);
            int intTodayDay = Convert.ToInt32(DateTime.Now.Day);
            txtDay.Text = Convert.ToString(intTodayDay);
            txtMonth.Text = Convert.ToString(intTodayMonth);
            txtYear.Text = Convert.ToString(intTodayYear);
            lblInfo.Text = "Today : " + CalendarConverter.ConvertChineseToGregorian(intTodayYear,intTodayMonth,intTodayDay);
        }

        //设置“Clear”按钮的单击事件，在单击后，进行以下工作：
        //1、清除日期和月份输入框中的内容
        //2、置年输入框的内容这“19”
        //3、将提示信息置为“Cleared”
        //4、将转换类型设置为“to Chinese Calendar”
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtDay.Text = "";
            txtMonth.Text = "";
            txtYear.Text = "19";
            cbbConvertClass.SelectedIndex = 0;
            lblInfo.Text = "Cleared.";
        }

        //设置“Convert”按钮的单击事件
        private void btnConverter_Click(object sender, EventArgs e)
        {
            //将输入日期全部置为0
            int intInputGregYear = 0;
            int intInputGregMonth = 0;
            int intInputGregDay = 0;
            try
            {
                //从输入框中读取相应的日期
                intInputGregYear = Convert.ToInt32(txtYear.Text);
                intInputGregMonth = Convert.ToInt32(txtMonth.Text);
                intInputGregDay = Convert.ToInt32(txtDay.Text);
            }
                //捕获异常FormatException
            catch (FormatException)
            {
                lblInfo.Text = "Format is Wrong!";
            }
                //捕获异常OverflowException
            catch (OverflowException)
            {
                lblInfo.Text = "Over Flow Exception!";
            }
                //捕获异常ArgumentException
            catch (ArgumentException)
            {
                lblInfo.Text = "the Argument Exception!";
            }
            finally
            {
                //如果输入的日期全部正常，则进行下一步工作
                //先检查用户需要输入的类型，是公历转农历，还是农历转公历
                if (cbbConvertClass.SelectedIndex == 1)
                    //如果是农历转公历
                {
                    //请读者自己完成该部分代码
                    lblInfo.Text = "Cann't Convert Gregorian Calendar to Chinese Calendar!";
                }
                else 
                    //如果是农历转公历
                {
                    lblInfo.Text = Convert.ToString(CalendarConverter.ConvertChineseToGregorian(intInputGregYear,intInputGregMonth,intInputGregDay));
                }
            }
        }
    }
}